package com.example.personalizededucationsystem.dto.response;

import com.example.personalizededucationsystem.dto.dto.QuestionInAssignmentDto;
import com.example.personalizededucationsystem.model.Assignment;
import lombok.Getter;
import lombok.Setter;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
public class AssignmentResponse {

    private Long assignmentId;
    private String title;
    private String description;
    private Instant createdAt;
    private Instant deadline;
    private Long classId;
    private String className;
    private String teacherName;
    private List<QuestionInAssignmentDto> questions;

    public static AssignmentResponse fromEntity(Assignment assignment) {
        AssignmentResponse dto = new AssignmentResponse();
        dto.setAssignmentId(assignment.getAssignmentId());
        dto.setTitle(assignment.getTitle());
        dto.setDescription(assignment.getDescription());
        dto.setCreatedAt(assignment.getCreatedAt());
        dto.setDeadline(assignment.getDeadline());

        if (assignment.getMyClass() != null) {
            dto.setClassId(assignment.getMyClass().getClassId());
            dto.setClassName(assignment.getMyClass().getClassName());
        }

        if (assignment.getTeacher() != null) {
            dto.setTeacherName(assignment.getTeacher().getUsername());
        }

        if (assignment.getAssignmentQuestions() != null) {
            dto.setQuestions(assignment.getAssignmentQuestions().stream()
                    .map(QuestionInAssignmentDto::fromEntity)
                    .collect(Collectors.toList()));
        }
        return dto;
    }

}